<?php
ini_set('display_errors', 0);
$appId = "wx0774ba4f6f80ec86";
$appSecrect = "168bf2e6ecddf7a17dc835793b0efc7a";
$token = "eTlre9R175DLNnbg8owJm1FHiYSSUJWN";
function http_post($url, $param = [], $post_file = false)
{
    $oCurl = curl_init();
    if (stripos($url, "https://") !== FALSE) {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);//CURL_SSLVERSION_TLSv1
    }
    if (is_string($param) || $post_file) {
        $strPOST = $param;
    } else {
        $aPOST = array();
        foreach ($param as $key => $val) {
            $aPOST[] = $key . "=" . urlencode($val);
        }
        $strPOST = join("&", $aPOST);
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($oCurl, CURLOPT_POST, true);
    curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if (intval($aStatus["http_code"]) == 200) {
        return $sContent;
    } else {
        return false;
    }
}

include_once './WxPayPubHelper/wechat.class.php';
$options = array(
    'token'=>'eTlre9R175DLNnbg8owJm1FHiYSSUJWN', //填写你设定的key
    'encodingaeskey'=>'U7iTD5hIVZlLwS1eDQh82plgge4Prb2FkYTDvSRajEm', //填写加密用的EncodingAESKey
    'appid'=>'wx0774ba4f6f80ec86', //填写高级调用功能的app id, 请在微信开发模式后台查询
    'appsecret'=>'168bf2e6ecddf7a17dc835793b0efc7a' //填写高级调用功能的密钥
);
$weObj = new Wechat($options); //创建实例对象

if(isset($_GET['code'])){
    $code = $_GET['code'];
    $appId = $appId;
    $secret = $appSecrect;
    $result = http_post("https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appId}&secret={$secret}&code={$code}&grant_type=authorization_code");
    $decodeRes = json_decode($result);
    $openid = $decodeRes->openid;

    $userInfo = $weObj->getUserInfo($openid);
    if($userInfo['subscribe'] == '0') {
      header('Location:wechat1.php');
      exit();
    }


}
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
</head>
<body>
<div id="aaa"></div>
<script>
window.onload = function(){
    window.localStorage.openid = '<?php echo $openid ? $openid : '';?>';
    window.localStorage.face = '<?php echo $userInfo['headimgurl'];?>';
    window.localStorage.unionid = '<?php echo $userInfo['unionid'];?>';
    window.localStorage.nickname = '<?php echo $userInfo['nickname'];?>';
    window.location = 'http://driver.tuniaoapp.com/?#/login'
   //document.getElementById('aaa').innerHTML = window.localStorage.openid
}
</script>
</body>
</html>
